package com.midea.ai.air.ui.user;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.midea.ai.air.R;
import com.midea.ai.air.common.BaseActivity;
import com.midea.ai.air.ui.home.MainActivityNew;
import com.midea.ai.air.view.MyTitleBar;

public class SetPasswordActivity extends BaseActivity implements View.OnClickListener {

    private EditText pw_one;
    private EditText pw_two;
    private TextView error_tv;
    private TextView actionbar_right_tv, actionbar_left_tv;
    private Dialog set_pw_Dialog;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 88:
                    if (set_pw_Dialog != null) {
                        set_pw_Dialog.dismiss();
                    }
                    startActivity(new Intent(SetPasswordActivity.this, MainActivityNew.class));
                    break;
                default:
                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setpassword);

        init();

    }

    MyTitleBar myTitleBar;

    private void init() {
        myTitleBar = new MyTitleBar(SetPasswordActivity.this);
        myTitleBar.setActionBar_center(R.string.set_password);
        myTitleBar.setActioBar_left(false, 0, R.string.dialog_cancel);
        myTitleBar.setActionBar_right(true, R.string.accomplish);

        actionbar_right_tv = (TextView) findViewById(R.id.actionbar_tv_right);
        actionbar_right_tv.setTextColor(getResources().getColor(R.color.electric_histogram));
        actionbar_left_tv = (TextView) findViewById(R.id.actionbar_tv_left);
        actionbar_left_tv.setTextColor(getResources().getColor(R.color.electric_histogram));
        actionbar_right_tv.setAlpha(0.25f);

        pw_one = (EditText) findViewById(R.id.set_password_one);
        pw_two = (EditText) findViewById(R.id.set_password_two);
        error_tv = (TextView) findViewById(R.id.set_password_error_tv);

        set_pw_Dialog = LoginActivity.createLoadingDialog(SetPasswordActivity.this, R.string.dialog_set_password);


        //忘记密码下一步按钮激活
        pw_two.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(pw_one.getText())) {
                    if (count < 0) {
                        actionbar_right_tv.setAlpha(0.25f);
                    } else {
                        actionbar_right_tv.setAlpha(1f);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.actionbar_left_relativeLayout:
                //返回按键
                finish();
                break;
            case R.id.actionbar_tv_right:
                //完成按钮
                setPassword();
                break;
            default:
                break;
        }

    }


    /**
     * 完成密码重置，登陆
     */
    private void setPassword() {
        String password_one = pw_one.getText().toString().trim();
        String password_two = pw_two.getText().toString().trim();
        error_tv.setTextColor(getResources().getColor(R.color.register_error_text));

        if (TextUtils.isEmpty(password_one)) {
            error_tv.setText(R.string.log_in_password_empty);
            return;
        }
        if (TextUtils.isEmpty(password_two)) {
            error_tv.setText(R.string.log_in_password_empty);
            return;
        }
        if (password_one.length() < 6 || password_one.length() > 16) {
            error_tv.setText(R.string.register_password_error);
            return;
        }

        if (LoginActivity.isSpecialCharacter(password_one)) {
            error_tv.setText(R.string.register_password_error);
            return;
        }
        LoginActivity.mLog("password_one = " + password_one + "::password_two = " + password_two);
        if (!password_one.equals(password_two)) {
            error_tv.setText(R.string.set_password_no_equal);
            return;
        }

        error_tv.setText("");

        set_pw_Dialog.show();
        handler.sendEmptyMessageDelayed(88, 1000);
    }

}
